import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import cm.word.generate.App;
import cm.word.generate.dao.pgsql.PgsqlQueryMapper;
import cm.word.generate.pojo.PgsqlWordDto;
import cm.word.generate.utils.JacobOperaword;

import java.util.List;

/**
 * @author caomin
 * @date 2018/5/7
 * @说明 pgsql生成
 */
@SpringBootTest(classes = App.class)
@RunWith(value = SpringRunner.class)
@SuppressWarnings("all")
public class PgsqlTestWord {


    @Autowired
    PgsqlQueryMapper pgsqlQueryMapper;

    @Test
    public void test() throws Exception {
        JacobOperaword jacobOperaword = new JacobOperaword(true);
        //创建文档
        jacobOperaword.createNewDocument();
        jacobOperaword.moveDown(10);
        List<String> zlpc = pgsqlQueryMapper.getTables();
        for (int i = 0; i < zlpc.size(); i++) {
            String tableName = zlpc.get(i);
            List<PgsqlWordDto> wordList = pgsqlQueryMapper.getColumnList(tableName);
            String tableComment = pgsqlQueryMapper.getTableComment(tableName);
            System.out.println(tableName);
            int tableIndex = i + 1;
            //创建表格的行数与列数
            int cols = 6, rows = wordList.size() + 1;
            String text = tableName + "表详细设计";
            String newText = tableComment + "(" + tableName + ")";
            jacobOperaword.insertText(newText);
            jacobOperaword.moveDown(2);
            jacobOperaword.moveRight(2);
            jacobOperaword.createTable(text, cols, rows);
            jacobOperaword.putTxtToCell(tableIndex, 1, 1, "代码");
            jacobOperaword.putTxtToCell(tableIndex, 1, 2, "名称");
            jacobOperaword.putTxtToCell(tableIndex, 1, 3, "数据类型");
            jacobOperaword.putTxtToCell(tableIndex, 1, 4, "是否为主键");
            jacobOperaword.putTxtToCell(tableIndex, 1, 5, "是否为空");
            jacobOperaword.putTxtToCell(tableIndex, 1, 6, "注释");
            for (int j = 0; j < wordList.size(); j++) {
                PgsqlWordDto pgsqlWordDto = wordList.get(j);
                jacobOperaword.putTxtToCell(tableIndex, j + 2, 1, pgsqlWordDto.getColumnName());
                jacobOperaword.putTxtToCell(tableIndex, j + 2, 2, pgsqlWordDto.getBz());
                jacobOperaword.putTxtToCell(tableIndex, j + 2, 3, pgsqlWordDto.getColumnType());

                //是否为主键
                String isPrimarkey = "FALSE";
                if (pgsqlWordDto.getColumnName().equalsIgnoreCase("id")) {
                    isPrimarkey = "TRUE";
                }
                jacobOperaword.putTxtToCell(tableIndex, j + 2, 4, isPrimarkey);

                //是否为空
                Boolean sfwk = pgsqlWordDto.isSfwk();
                String sfwkString = "FALSE";
                if (sfwk != null && sfwk == true) {
                    sfwkString = "TRUE";
                }
                jacobOperaword.putTxtToCell(tableIndex, j + 2, 5, sfwkString);
                jacobOperaword.putTxtToCell(tableIndex, j + 2, 6, pgsqlWordDto.getBz());
            }
            jacobOperaword.moveDown(3);
            jacobOperaword.insertText("我是华丽的分隔线");
        }
        jacobOperaword.save("d:/1023test.doc");
        jacobOperaword.setSaveOnExit(true);
        jacobOperaword.closeDocumentWithoutSave();
        jacobOperaword.closeDocument();
        jacobOperaword.close();
    }


}



